package com.erbantou.filemanage.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * WebSocket配置类，启用WebSocket消息代理并进行相关配置
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * 配置消息代理
     *
     * @param registry 消息代理配置对象
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 启用简单的消息代理，代理目的地前缀为 /topic 和 /queue
        registry.enableSimpleBroker("/topic", "/queue");
        // 设置应用程序的消息目的地前缀为 /app
        registry.setApplicationDestinationPrefixes("/app");
    }

    /**
     * 注册 STOMP 端点，启用 SockJS 后备选项
     *
     * @param registry STOMP端点注册对象
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册STOMP端点，客户端将使用此端点连接到 WebSocket 服务器
        registry.addEndpoint("/ws")
                .withSockJS();
    }
}
